package dbzx.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPHeaderCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfTest {
	
	
	
	
	
	/** windows字体路径 */
	public static final String FONT = "C:/WINDOWS/Fonts/simhei.ttf";
	/** 保存文件夹的路径 */
	public static final String BASE_DEST = "E:/项目资料/pdfCreat/";
	
	public static final String IMG_ONE = "src/main/resources/img/pdf/xiaolan2.bmp";
	public static void main(String[] args) throws DocumentException, IOException {
		
		String path = BASE_DEST + String.valueOf(System.currentTimeMillis())+".pdf";
//		BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); 
//        // 正文的字体
//		BaseColor fontColor1 = new BaseColor(117, 143, 199);//自定义字体颜色
//		Font onecPageFone = new Font(bf,40f,Font.BOLD,BaseColor.BLACK);
//        Font headFont = new Font(bf, 14f, Font.BOLD, BaseColor.BLUE);
//        Font textFont = new Font(bf, 10f, Font.NORMAL, fontColor1);
		new PdfTest().createPdf(path);
	}
	
	public void createPdf(String path) throws DocumentException, MalformedURLException, IOException {
		Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 20);
		Font headFont = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9);
        Document doc = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path));
//        writer.setTagged();
//        writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
        
        /** 添加页眉事件 */
        HeadEvent headEvent = new HeadEvent();
        writer.setPageEvent(headEvent);
        
        
        doc.open();
        doc.addLanguage("zh-CN");
        doc.addTitle("中文标题");
        
        Paragraph p = new Paragraph();
        
        
        
        List list = new List(true);
        list.add(new ListItem("quick", font));
        list.add(new ListItem("brown", font));
        list.add(new ListItem("fox", font));
        list.add(new ListItem("jumps", font));
        list.add(new ListItem("over", font));
        list.add(new ListItem("the", font));
        list.add(new ListItem("lazy", font));
        list.add(new ListItem("dog", font));
        doc.add(list);
        
        
        Paragraph ppp = new Paragraph("手机技术开发；手机生产、手机服务；（限海淀区永捷北路2号二 层经营）从事互联网文化活动；技术开发；货物进出口、技术进出 口、代理进出口；销售通讯设备；维修仪器仪表；维修办公设备 ；承办展览展示活动；会议服务；筹备、策划、组织大型庆典；设 计、制作、代理、发布广告。（企业依法自主选择经营项目，开展 经营活动；依法须经批准的项目，经相关部门批准后依批准的内容 开展经营活动；不得从事本市产业政策禁止和限制类项目的经营活 动。"
        		,font);
        ppp.setMultipliedLeading(3);
//        ppp.setFont(font);
        doc.add(ppp);
        //向段落中添加文本块
        Chunk c = new Chunk("这是个文本块！！");
        p.add(c);
        PdfPTable table = new PdfPTable(2);
        table.addCell(ppp);
        table.addCell(ppp);
        doc.add(table);
        //向段落中添加图片
        Image i= Image.getInstance(IMG_ONE);
        /** 参数设置xy轴的相对偏移量，相对上一个元素开始，x轴上：距离上一个元素40个像素，y轴 上：展示距离上一个元素370个元素。*/
        c = new Chunk(i, 40, -370);
        c.setAccessibleAttribute(PdfName.ALT, new PdfString("xiaolan"));
        p.add(c);
        
        doc.add(p);
        
        
        p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n", font);
        doc.add(p);
        
        list = new List(true);
        list.add(new ListItem("quick", font));
        list.add(new ListItem("brown", font));
        list.add(new ListItem("fox", font));
        list.add(new ListItem("jumps", font));
        list.add(new ListItem("over", font));
        list.add(new ListItem("the", font));
        list.add(new ListItem("lazy", font));
        list.add(new ListItem("dog", font));
        doc.add(list);
        doc.newPage();
        doc.add(list);
        doc.close();
        System.out.println("pdf生成！！！");
	}
	
	
	class HeadEvent extends PdfPageEventHelper {
		@Override
		public void onEndPage(PdfWriter writer, Document doc) {
			Font headFont = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9);
			//添加标题文本
	        ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_RIGHT,new Phrase("国家技术转移东部中心",headFont),doc.right()-20,doc.top(),0); 
	        super.onEndPage(writer, doc);
		}
		
	}
	
}



 
